package com.apestech.framework.xml.plugin.node;

import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.plugin.callback.ExecNodeCallback;
import com.apestech.framework.xml.plugin.modul.Model;
import com.apestech.framework.xml.pub.core.ElementUtil;
import org.dom4j.Element;

import java.util.List;

/**
 * @author hup
 * @create 2019-01-26-16:46
 */
public class NodeModel extends ElementUtil {
    private Element node;
    private Model model=new Model();

    public NodeModel(Element node){
        this.node=node;
    }

    public void getAllModel() throws Exception {
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                Element ele=(Element) list.get(i);
                execSubChild(ele, new ExecNodeCallback(){
                    @Override
                    public void getNode(Element e, String id, JSONObject attr, String text) throws Exception {
                        model.setModel(id, attr);
                        getModelList(id, e);
                    }
                });
            }
        }
    }

    private void getModelList(String id, Element e) throws Exception {
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                model.addField(id, getNodeAttr(eml).getJSONObject("attrNode"));
            }
        }
    }

    public Model getModel() {
        return model;
    }

}
